# -*- coding: latin-1 -*-
#=========================================================================================
from Code.Constantes import *

import os

from PyQt4 import QtGui

import Code.Partida as Partida

import Code.QT.Iconos as Iconos
import Code.QT.Controles as Controles
import Code.QT.Colocacion as Colocacion
import Code.QT.QTUtil as QTUtil
import Code.QT.QTUtil2 as QTUtil2
import Code.QT.FormLayout as FormLayout
import Code.QT.Grid as Grid
import Code.QT.Columnas as Columnas

import Code.QT.WBG_Comun as WBG_Comun

#=========================================================================================
class WSummary( QtGui.QWidget ) :
    def __init__( self, winBookGuide, dbGames ) :
        QtGui.QWidget.__init__( self )

        self.winBookGuide = winBookGuide

        self.dbGames = dbGames# <--setdbGames
        self.bookGuide = winBookGuide.bookGuide
        self.infoMove = None # <-- setInfoMove
        self.wmoves = None # <-- setwmoves
        self.fenM2 = None
        self.liMoves = []
        self.analisisMRM = None

        self.pvBase = ""

        self.orden = ["games", False]

        # Grid
        oColumnas = Columnas.ListaColumnas()
        oColumnas.nueva( "move", _( "Move" ), 60 )
        oColumnas.nueva( "analisis", _("Analysis"), 60, siDerecha=True )
        oColumnas.nueva( "games", _( "Games" ), 70, siDerecha=True )
        oColumnas.nueva( "pgames", "% "+_( "Games" ), 70, siDerecha=True, siCentrado=True )
        oColumnas.nueva( "white", _( "White" ), 70, siDerecha=True )
        oColumnas.nueva( "draw", _( "Draw" ), 70, siDerecha=True )
        oColumnas.nueva( "black", _( "Black" ), 70, siDerecha=True )
        oColumnas.nueva( "pwhite", "% "+_( "White" ), 60, siDerecha=True )
        oColumnas.nueva( "pdraw", "% "+_( "Draw" ), 60, siDerecha=True )
        oColumnas.nueva( "pblack", "% "+_( "Black" ), 60, siDerecha=True )
        oColumnas.nueva( "pdrawwhite", "%% %s"%_( "W+D"), 60, siDerecha=True )
        oColumnas.nueva( "pdrawblack", "%% %s"%_( "B+D"), 60, siDerecha=True )

        self.grid = Grid.Grid( self, oColumnas, id="summary", siSelecFilas=True )

        # ToolBar
        liAcciones = [  ( _( "Previous" ), Iconos.Anterior(), "anterior" ),
                        ( _( "Next" ), Iconos.SiguienteF(), "siguiente" ), None,
                        ( _( "Analyze"), Iconos.Analizar(), "analizar" ), None,
                        100, None,
                        ( _( "Create a new guide based in these games" ), Iconos.BookGuide(), "create" ), None,
                        ( _( "Filter games" ), Iconos.Filtrar(), "filtrar" ), None,
                    ]
        self.tb = Controles.TB( self, liAcciones, tamIcon = 16, siTexto = False )

        layout = Colocacion.V().control(self.tb).control(self.grid).margen(1)

        self.setLayout(layout)

        self.qtColor = ( QTUtil.qtColorRGB( 221,255,221 ), QTUtil.qtColorRGB( 247,247,247 ), QTUtil.qtColorRGB( 255,217,217 ) )

    #--------------------------------------------------------------------------------------------------------------------------------
    def gridDobleClickCabecera( self, grid, oColumna ) :
        clave = oColumna.clave

        if clave == "analisis" :
            func = lambda dic: dic["analisis"].puntosABS() if dic["analisis"] else -9999999
        elif clave == "move" :
            func = lambda dic: dic["move"].upper()
        else :
            func = lambda dic: dic[clave]
        li = sorted(self.liMoves, key=func)

        orden,mas = self.orden
        if orden == clave :
            mas = not mas
        else :
            mas = clave == "move"
        if not mas :
            li.reverse()
        self.orden = clave, mas
        self.liMoves = li
        self.grid.refresh()

    #--------------------------------------------------------------------------------------------------------------------------------
    def setdbGames( self, dbGames ) :
        self.dbGames = dbGames

    #--------------------------------------------------------------------------------------------------------------------------------
    def focusInEvent( self, event ) :
        self.winBookGuide.ultFocus = self

    #--------------------------------------------------------------------------------------------------------------------------------
    def setInfoMove( self, infoMove ) :
        self.infoMove = infoMove

    #--------------------------------------------------------------------------------------------------------------------------------
    def setwmoves( self, wmoves ) :
        self.wmoves = wmoves

    #--------------------------------------------------------------------------------------------------------------------------------
    def gridNumDatos( self, grid ) :
        return len(self.liMoves)

    #--------------------------------------------------------------------------------------------------------------------------------
    def gridDato( self, grid, nfila, ocol ) :
        clave = ocol.clave
        v = self.liMoves[nfila][clave]
        if clave == "move" :
            return v
        elif clave.startswith( "p" ) :
            return "%.01f %%"%v
        elif clave == "analisis" :
            return v.abrTextoBase() if v else ""
        else :
            return str(v)

    #--------------------------------------------------------------------------------------------------------------------------------
    def posicionFila( self, nfila ) :
        dic = self.liMoves[nfila]
        li = [ [k,dic[k]] for k in ("white","draw","black") ]
        li = sorted(li, key=lambda x: x[1], reverse=True)
        d = {}
        prev = 0
        ant = li[0][1]
        for cl, v in li :
            if v < ant :
                prev += 1
            d[cl] = prev
            ant = v
        return d

    #--------------------------------------------------------------------------------------------------------------------------------
    def gridColorFondo( self, grid, nfila, ocol ) :
        clave = ocol.clave
        if clave in ( "pwhite", "pdraw", "pblack" ) :
            dic = self.posicionFila( nfila )
            n = dic[clave[1:]]
            return self.qtColor[n]

        return None

    #--------------------------------------------------------------------------------------------------------------------------------
    def procesarTB( self ) :
        getattr( self, self.sender().clave )()

    #--------------------------------------------------------------------------------------------------------------------------------
    def popPV( self, pv ) :
        if pv :
            rb = pv.rfind(" ")
            if rb == -1 :
                pv = ""
            else :
                pv = pv[:rb]
        return pv

    #--------------------------------------------------------------------------------------------------------------------------------
    def analizar( self ) :
        if not self.fenM2 :
            return

        fila = self.grid.recno()
        wk = self.liMoves[fila]
        pv = wk["pv"]

        rmAnalisis = wk["analisis"]
        siShowMoves = rmAnalisis is not None

        def dispatch( nada ) :
            self.actualizaPV(self.pvBase)

        analisis = WBG_Comun.Analisis(self, self.bookGuide, dispatch)
        resp = analisis.menuAnalizar( self.fenM2, None, siShowMoves )
        if resp :
            partida = wk["partida"]
            fen = partida.jugada(-1).posicionBase.fen()
            pv = wk["pv"]
            analisis.exeAnalizar( self.fenM2, resp, None, fen, pv, rmAnalisis )

    #--------------------------------------------------------------------------------------------------------------------------------
    def anterior( self ) :
        if self.pvBase :
            pv = self.popPV(self.pvBase)
        else :
            pv = None
        self.actualizaPV(pv)
        self.cambiaInfoMove()

    #--------------------------------------------------------------------------------------------------------------------------------
    def siguiente( self ) :
        recno = self.grid.recno()
        if recno >= 0 :
            pv = self.liMoves[recno]["pv"]
            self.actualizaPV( pv )
            self.cambiaInfoMove()

    #--------------------------------------------------------------------------------------------------------------------------------
    def create( self ) :
        name = os.path.basename(self.dbGames.nomFichero)[:-5]
        maxdepth = self.dbGames.depthStat()
        depth = 5
        minGames = min(self.dbGames.reccountTotal()*10/100,5)
        mxW = True
        mxD = True
        mxB = True

        while True :
            liGen = [(None,None)]
            liGen.append( ( _( "Name" ) + " :", name ) )

            liGen.append( (FormLayout.Spinbox( _( "Depth" ), 2, maxdepth, 50 ),depth) )

            liGen.append( (None,None) )

            liGen.append( (FormLayout.Spinbox( _("Minimum games"), 1, 99, 50 ),minGames) )

            liGen.append( (None,None) )
            liGen.append( (_("Maximum % White"),mxW) )
            liGen.append( (_("Maximum % Draw"),mxD) )
            liGen.append( (_("Maximum % Black"),mxB) )

            resultado = FormLayout.fedit( liGen, title=_("Create new guide"), parent = self, anchoMinimo = 460, icon = Iconos.TutorialesCrear() )
            if resultado is None :
                return

            accion, liResp = resultado
            name = liResp[0].strip()
            if not name :
                return
            depth = liResp[1]
            minGames = liResp[2]
            mxW = liResp[3]
            mxD = liResp[4]
            mxB = liResp[5]

            active, dicL = self.bookGuide.getConfiguration()
            ok = True
            for k, v in dicL.iteritems() :
                if v.upper() == name.upper() :
                    QTUtil2.mensError(self, _("The name is repeated") )
                    ok = False
                    continue
            if ok :
                break

        # Grabamos
        um = QTUtil2.unMomento(self)

        # Read stats
        fichPVs = self.dbGames.flistAllpvs(depth, minGames, mxW, mxD, mxB )

        # Write to bookGuide
        self.bookGuide.grabarFichSTAT( fichPVs )

        # BookGuide
        self.wmoves.inicializa()

        um.final()

    #--------------------------------------------------------------------------------------------------------------------------------
    def filtrar( self ) :
        pass

    #--------------------------------------------------------------------------------------------------------------------------------
    def movActivo( self ) :
        recno = self.grid.recno()
        if recno >= 0 :
            return self.liMoves[recno]
        else :
            return None

    #--------------------------------------------------------------------------------------------------------------------------------
    def gridDobleClick( self, grid, fil, col ) :
        self.siguiente()

    #--------------------------------------------------------------------------------------------------------------------------------
    def actualiza( self ) :
        movActual = self.infoMove.movActual
        if movActual and movActual != self.bookGuide.root :
            pvBase = self.popPV(movActual.allPV())
        else :
            pvBase = None
        self.actualizaPV( pvBase )
        if movActual :
            pv = movActual.allPV()
            for n, uno in enumerate(self.liMoves) :
                if uno["pv"] == pv :
                    self.grid.goto(n,0)
                    return

    #--------------------------------------------------------------------------------------------------------------------------------
    def actualizaPV( self, pvBase ) :
        self.pvBase = pvBase
        if pvBase is None :
            pvMirar = None
        else :
            pvMirar = self.pvBase

        self.analisisMRM = None
        dicAnalisis = {}
        self.fenM2 = None
        if pvMirar is not None:
            p = Partida.Partida()
            if pvMirar :
                p.leerPV(pvMirar)
            self.fenM2 = p.ultPosicion.fenM2()
            self.analisisMRM = self.bookGuide.analisisMRM(self.fenM2)
            if self.analisisMRM :
                for rm in self.analisisMRM.liMultiPV :
                    dicAnalisis[ rm.movimiento() ] = rm
        self.liMoves = self.dbGames.getSummary( pvMirar, dicAnalisis )

        self.grid.refresh()
        self.grid.gotop()

    #--------------------------------------------------------------------------------------------------------------------------------
    def gridCambiadoRegistro( self, grid, fila, oCol ) :
        if self.grid.hasFocus() or self.hasFocus():
            self.cambiaInfoMove()
    #--------------------------------------------------------------------------------------------------------------------------------
    def cambiaInfoMove( self ) :
        fila = self.grid.recno()
        if fila >= 0 :
            pv = self.liMoves[fila]["pv"]
            p = Partida.Partida()
            p.leerPV( pv )
            p.compruebaFinal()
            self.infoMove.modoPartida(p,9999)
            self.setFocus()
            self.grid.setFocus()
